package find.prj.lib;

public class SortedMegaList extends MegaList<Integer> {

	@Override
	public boolean add(Integer value) {
		if (this.isEmpty())
			this.FirstNode = new ListNode<Integer>(value);
		else {
			ListNode<Integer> checkNode = this.FirstNode;
			ListNode<Integer> prevNode = null;
			ListNode<Integer> newNode = new ListNode<Integer>(value);
			while (checkNode.NextNode != null) {
				
				if(value < checkNode.getValue()) {
					if (prevNode != null)
						prevNode.NextNode = newNode;
					else
						this.FirstNode = newNode;
					newNode.NextNode = checkNode;
					break;
				}
				prevNode = checkNode;
				checkNode = checkNode.NextNode;
			}
			if (checkNode.NextNode == null) {
				checkNode.NextNode = newNode;
				newNode.NextNode = null; 
			}
		}
		this.Size++;
		return true;
	}

}
